<?php

namespace Entities;

use Doctrine\ORM\Mapping as ORM;

use Symfony\Component\Validator\Validator;
use Symfony\Component\Validator\Mapping\ClassMetadataFactory;
use Symfony\Component\Validator\Mapping\Loader\StaticMethodLoader;
use Symfony\Component\Validator\Constraints as Assert;
use Symfony\Component\Validator\ConstraintValidatorFactory;



/**
 * Entities\Stem
 */
class Stem
{
    /**
     * @var string $name
     */
    private $name;
    
    /**
     * @var integer $value
     */
    private $value;

    /**
     * @var smallint $active
     */
    private $active;

    /**
     * @var integer $id
     */
    private $id;
    
    /**
     * Set name
     *
     * @param string $name
     * @return Stem
     */
    public function setName($name)
    {
        $this->name = $name;
        
   	    $this->validate('name');
        
        return $this;
    }

    /**
     * Get name
     *
     * @return string 
     */
    public function getName()
    {
        return $this->name;
    }
    
    
    
    /**
     * Get value
     *
     * @return integer
     */
    public function getValue() 
    {
        return $this->value;
    }
    
    
    
    /**
     * Set value
     *
     * @param integer $value
     * @return Stem
     */
    public function setValue($value) 
    {
        $this->value = intval($value);
        return $this;
    }
    
    

    /**
     * Set active
     *
     * @param smallint $active
     * @return Stem
     */
    public function setActive($active)
    {
        $this->active = ($active === true) ? true : false;
        return $this;
    }

    /**
     * Get active
     *
     * @return smallint 
     */
    public function getActive()
    {
        $active = false;
        
        if ($this->active == 1) {
            $active = true;
        }
        
        return $active;
    
    } // public function getActive()



    /**
     * Get id
     *
     * @return integer 
     */
    public function getId()
    {
        return $this->id;
    }
    
    
    
    /**
     *
     *
     */
    protected function validate($variableName)
    {
	    $result = false;
	    
	    $validator = new Validator(
			        new ClassMetadataFactory(new StaticMethodLoader()),
			        new ConstraintValidatorFactory()
			    );
	    
	    $constraint = new Assert\Collection(array(
				'name' => array(
				  			new Assert\NotBlank(),
				  			new Assert\MinLength(2),
				  			new Assert\MaxLength(18),
							),
				));
	    
		$violations = $validator->validateValue(array($variableName => $this->$variableName), $constraint);
/* 		$violations = $validator->validateValue(array('name' => $this->name), $constraint); */
/* var_dump($violations);	     */
	    
	    return $result;
	    
    }
    
    
    
}